//
//  HPPosNumberKeyBoardView.m
//  V-POS
//
//  Created by handpay on 12-11-9.
//  Copyright (c) 2012年 handpay. All rights reserved.
//

#import "VPOSNumberKeyBoardView.h"
#import "Utility.h"
#import "CategoryUtil.h"

@interface VPOSNumberKeyBoardView ()
{
    VPOSKeyboardType type;
}

@end

#define kSeflFrame    CGRectMake(0, 0, 320, 216)

@implementation VPOSNumberKeyBoardView

@synthesize delegate;

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    if (type == VPOSKeyboardType_NUMBER) {
        [self removeFromSuperview];
    }
    
    [super dealloc];
}

- (id)initKeyboardWithType:(VPOSKeyboardType)type_ delegate:(id<VPOSNumberKeyBoardViewDelegate>)delegate_ frame:(CGRect)frame_{    
    self = [super initWithFrame:frame_];
    if (self) {
        [self setAutoresizesSubviews:NO];
        
        type = type_;
        
        self.delegate = delegate_;
        
        NSArray *tempNumberArray = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"0", @".", @"  ", nil];
		NSArray* numberArray = [tempNumberArray randomizedArray];
        
        int interval = 6, currentX, currentY = 0;
        
        int numberButtonWidth = 64, funcButtonWidth = 91, buttonHeight = 46;
        
        int currentRow = 0, currentColumn = 0;
        
        for (int i = 0; i < numberArray.count; i ++) {
            NSString *numberStr = [numberArray objectAtIndex:i];
            
            if ((i + 1) % 3 == 1) {
                currentRow ++;
                currentColumn = 1;
            } else {
                currentColumn ++;
            }
            
            currentX = currentColumn * interval + (currentColumn - 1) * numberButtonWidth;
            
            currentY = (currentRow - 1) * (buttonHeight + interval) + 10;
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setExclusiveTouch:YES];
            [button.titleLabel setFont:[UIFont boldSystemFontOfSize:24.]];
            UIImage *bgImage = nil;
            if (i == 4) {
                bgImage = [UIImage imageNamed:@"number_5_bg.png"];
            } else {
                bgImage = [UIImage imageNamed:@"number_normal_bg.png"];
            }
            [button setBackgroundImage:bgImage forState:UIControlStateNormal];
            if (type == VPOSKeyboardType_PIN) {
                [button setBackgroundImage:bgImage forState:UIControlStateHighlighted];
            }
            [button setBackgroundImage:bgImage forState:UIControlStateDisabled];
            [button setTitle:numberStr forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
            [button setFrame:CGRectMake(currentX, currentY, numberButtonWidth, buttonHeight)];
            [button addTarget:self action:@selector(touchNumberButton:) forControlEvents:UIControlEventTouchUpInside];
            if (type_ == VPOSKeyboardType_PIN && ([numberStr isEqualToString:@"."] || [numberStr isEqualToString:@"  "])) {
                [button setEnabled:NO];
            }else if(type_ == VPOSKeyboardType_CARDNO && [numberStr isEqualToString:@"."]){
                [button setEnabled:NO];
            }
            [self addSubview:button];
        }
        
        currentX = 3 * (numberButtonWidth + interval) + 13;
        
        [self addSubview:[self createFuncButtonWithFrame:CGRectMake(currentX, 10, funcButtonWidth, buttonHeight) imageName:@"function_cancel.png" action:@selector(touchCancelButton)]];
        
        [self addSubview:[self createFuncButtonWithFrame:CGRectMake(currentX, (1 * (buttonHeight + interval) + 10), funcButtonWidth, buttonHeight) imageName:@"function_correct.png" action:@selector(touchCorrectButton)]];
        
        [self addSubview:[self createFuncButtonWithFrame:CGRectMake(currentX, (2 * (buttonHeight + interval) + 10), funcButtonWidth, buttonHeight) imageName:@"closetext" action:@selector(touchCloseButton)]];
        
        [self addSubview:[self createFuncButtonWithFrame:CGRectMake(currentX, (3 * (buttonHeight + interval) + 10), funcButtonWidth, buttonHeight) imageName:@"function_confirm.png" action:@selector(touchOKButton)]];
        
        if (type_ == VPOSKeyboardType_NUMBER
            || type_ == VPOSKeyboardType_CARDNO) {
            self.frame = kSeflFrame;
            [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"keyboard_bg.png"]]];
        }
        
    }
    
    return self;
}

//- (id)initPinKeyboardWithDelegate:(id<VPOSNumberKeyBoardViewDelegate>)delegate_ frame:(CGRect)frame_
//{
//    return [self initWithPwdType:VPOSKeyboardType_PIN delegate:delegate_ frame:frame_];
//}
//
//- (id)initNumberKeyboardWithDelegate:(id<VPOSNumberKeyBoardViewDelegate>)delegate_
//{
//    self = [self initWithPwdType:VPOSKeyboardType_NUMBER delegate:delegate_ frame:kSeflFrame];
//    if (self) {
//        [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"keyboard_bg.png"]]];
//    }
//    return self;
//}
//
//- (id)initWithPwdType:(VPOSKeyboardType)type_ delegate:(id<VPOSNumberKeyBoardViewDelegate>)delegate_ frame:(CGRect)frame_
//{
//    self = [super initWithFrame:frame_];
//    if (self) {
//        [self setAutoresizesSubviews:NO];
//        
//        type = type_;
//        
//        self.delegate = delegate_;
//        
//        NSArray *numberArray = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"0", @".", @"00", nil];
//        
//        int interval = 6, currentX, currentY = 0;
//        
//        int numberButtonWidth = 64, funcButtonWidth = 91, buttonHeight = 46;
//        
//        int currentRow = 0, currentColumn = 0;
//        
//        for (int i = 0; i < numberArray.count; i ++) {
//            NSString *numberStr = [numberArray objectAtIndex:i];
//            
//            if ((i + 1) % 3 == 1) {
//                currentRow ++;
//                currentColumn = 1;
//            } else {
//                currentColumn ++;
//            }
//            
//            currentX = currentColumn * interval + (currentColumn - 1) * numberButtonWidth;
//            
//            currentY = (currentRow - 1) * (buttonHeight + interval) + 10;
//            
//            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//            [button.titleLabel setFont:[UIFont boldSystemFontOfSize:24.]];
//            UIImage *bgImage = nil;
//            if ([numberStr isEqualToString:@"5"]) {
//                bgImage = [UIImage imageNamed:@"number_5_bg.png"];
//            } else {
//                bgImage = [UIImage imageNamed:@"number_normal_bg.png"];
//            }
//            [button setBackgroundImage:bgImage forState:UIControlStateNormal];
//            if (type == VPOSKeyboardType_PIN) {
//                [button setBackgroundImage:bgImage forState:UIControlStateHighlighted];
//            }
//            [button setBackgroundImage:bgImage forState:UIControlStateDisabled];
//            [button setTitle:numberStr forState:UIControlStateNormal];
//            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//            [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
//            [button setFrame:CGRectMake(currentX, currentY, numberButtonWidth, buttonHeight)];
//            [button addTarget:self action:@selector(touchNumberButton:) forControlEvents:UIControlEventTouchUpInside];
//            if (type_ == VPOSKeyboardType_PIN && ([numberStr isEqualToString:@"."] || [numberStr isEqualToString:@"00"])) {
//                [button setEnabled:NO];
//            }else if(type_ == VPOSKeyboardType_CARDNO && [numberStr isEqualToString:@"."]){
//                [button setEnabled:NO];
//            }
//            [self addSubview:button];
//        }
//        
//        currentX = 3 * (numberButtonWidth + interval) + 13;
//        
//        [self addSubview:[self createFuncButtonWithFrame:CGRectMake(currentX, 10, funcButtonWidth, buttonHeight) imageName:@"function_cancel.png" action:@selector(touchCancelButton)]];
//        
//        [self addSubview:[self createFuncButtonWithFrame:CGRectMake(currentX, (1 * (buttonHeight + interval) + 10), funcButtonWidth, buttonHeight) imageName:@"function_correct.png" action:@selector(touchCorrectButton)]];
//        
//        [self addSubview:[self createFuncButtonWithFrame:CGRectMake(currentX, (2 * (buttonHeight + interval) + 10), funcButtonWidth, buttonHeight) imageName:nil action:nil]];
//        
//        [self addSubview:[self createFuncButtonWithFrame:CGRectMake(currentX, (3 * (buttonHeight + interval) + 10), funcButtonWidth, buttonHeight) imageName:@"function_confirm.png" action:@selector(touchOKButton)]];
//    }
//    return self;
//}

- (UIButton *) createFuncButtonWithFrame:(CGRect)frame imageName:(NSString *)imageName action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setExclusiveTouch:YES];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:24.]];
    [button setBackgroundImage:[UIImage imageNamed:@"function_normal_bg.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"function_normal_bg.png"] forState:UIControlStateDisabled];
    [button setBackgroundImage:[UIImage imageNamed:@"function_normal_bg.png"] forState:UIControlStateHighlighted];
    if (imageName) {
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        if (type == VPOSKeyboardType_PIN) {
            [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
        }
    }
    [button setFrame:frame];
    if (action) {
        [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    } else {
        [button setEnabled:NO];
    }
    return button;
}

/**
 * 点击数字按钮事件
 */
- (void) touchNumberButton:(UIButton *)button
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(inputView:notifyInputString:)]) {
        [self.delegate inputView:[Utility currentResponseView:[UIApplication sharedApplication].keyWindow] notifyInputString:button.titleLabel.text];
    }
}

/**
 * 点击取消按钮事件
 */
- (void) touchCancelButton
{
    UIControl *inputView = [Utility currentResponseView:[UIApplication sharedApplication].keyWindow];
    if (self.delegate && [self.delegate respondsToSelector:@selector(responseCancelButtonWithInputView:)]) {
        [self.delegate responseCancelButtonWithInputView:inputView];
        return;
    }
    if ([inputView isKindOfClass:[UITextField class]]) {
        UITextField *textField = (UITextField *)inputView;
        if (textField.text.length > 0) {
            [textField setText:@""];
        }
    }
}

/**
 * 点击更正按钮事件
 */
- (void) touchCorrectButton
{
    UIControl *inputView = [Utility currentResponseView:[UIApplication sharedApplication].keyWindow];
    if (self.delegate && [self.delegate respondsToSelector:@selector(responseCorrectButtonWithInputView:)]) {
        [self.delegate responseCorrectButtonWithInputView:inputView];
        return;
    }
    if ([inputView isKindOfClass:[UITextField class]]) {
        UITextField *textField = (UITextField *)inputView;
        if (textField.text.length > 0) {
            [textField setText:[textField.text substringToIndex:(textField.text.length - 1)]];
        }
    }
}

/**
 * 点击确认按钮事件
 */
- (void) touchOKButton
{
    self.userInteractionEnabled = NO;
    [self performSelector:@selector(recoverTouch) withObject:nil afterDelay:.3];
    if (self.delegate && [self.delegate respondsToSelector:@selector(responseOKButtonWithInputView:)]) {
        [self.delegate responseOKButtonWithInputView:[Utility currentResponseView:[UIApplication sharedApplication].keyWindow]];
    }
}

- (void) touchCloseButton
{
    self.userInteractionEnabled = NO;
    if (self.delegate && [self.delegate respondsToSelector:@selector(responseCloseButtonWithInputView:)]) {
        [self.delegate responseCloseButtonWithInputView:[Utility currentResponseView:[UIApplication sharedApplication].keyWindow]];
    }
}

- (void)recoverTouch
{
    self.userInteractionEnabled = YES;
}


@end
